package com.example.boos.util;

import android.os.Message;
import android.util.Log;

import com.example.boos.MainActivity;
import com.example.boos.pojo.Orders;
import com.example.boos.ui.notifications.NotificationsFragment;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.tools.jsonrpc.JacksonJsonRpcMapper;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class MyRabbitUtil {
    public static String host = "121.196.42.169";
    public static int port = 5672;
    public static String userName = "waimai";
    public static String password = "123456";
    public static String virtualHost = "waimaiHost";

    public static Boolean run = true;

    private static Connection connection;
    private static Channel channel;

    private static void getChannel(){
        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(host);
        factory.setPort(port);
        factory.setUsername(userName);
        factory.setPassword(password);
        factory.setVirtualHost(virtualHost);
        try {
            connection = factory.newConnection();
            channel = connection.createChannel();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }

    public static void close(){
        try {
            channel.close();
            connection.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

    }

    // 消费消息
    public static void handlerMessage(String exchange,String ... routeKey){
        Log.e("消息","消息测试");
        MyThreadPool.doJob(new Runnable() {
            @Override
            public void run() {
                while(run){
                    // 声明交换机
                    try {
                        getChannel();
                        System.out.println(exchange);
                        channel.exchangeDeclare(exchange,"direct");
                        // 直接创建一个队列吧
                       /* String queue = channel.queueDeclare().getQueue();*/
                        String queue = channel.queueDeclare(routeKey[0],true,false,false,null).getQueue();

                        // 队列指定Route key
                        for(String key:routeKey){
                            channel.queueBind(queue,exchange,key);
                        }
                        // 消费消息
                        while(run){
                            channel.basicQos(1);// 一次只接受一条未确认的消息
                            channel.basicConsume(queue,false,new DefaultConsumer(channel){
                                @Override
                                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                                    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
                                    String json = new String(body);
                                    Orders order = gson.fromJson(json,Orders.class);
                                    Message msg = new Message();
                                    if(order.getUserStatus() != -1){
                                        msg.what = 8;
                                    }else{
                                        msg.what = 10;
                                    }
                                    msg.obj = order;
                                    Log.e("服务测试","处理消息"+order.getTotalPrice());
                                    Log.e("服务测试","谁处理了该条消息"+queue);
                                    MainActivity.handler.sendMessage(msg);
                                    channel.basicAck(envelope.getDeliveryTag(),false); // 手动确认消息
                                }
                            });
                        }
                        return ;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        close();
                    }
                }
            }
        });

    }

}
